package com.xxwu.springlibrary.model;

import com.xxwu.springlibrary.enums.ResultStatus;
import lombok.Data;

@Data
public class Result<T> {
    private int code;
    private String message;
    private T data; // 使用Object则需要进行强制类型转化，T 则可以直接接收

    /**
     * 登陆成功时
     * @param data
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultStatus.SUCCESS.getCode());
        result.setData(data);
        return result;
    }

    /**
     * 登陆失败时，可有参数也可没有
     * @param data
     * @param message
     * @return
     * @param <T>
     */
    public static <T> Result<T> fail(T data, String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultStatus.FAILED.getCode());
        result.setData(data);
        result.setMessage(message);
        return result;
    }
    public static <T> Result<T> fail(String message) {
        Result<T> result = new Result<>();
        result.setCode(ResultStatus.FAILED.getCode());
        result.setMessage(message);
        return result;
    }

    /**
     * 用户未登录
     * @return
     * @param <T>
     */
    public static <T> Result<T> unLogin() {
        Result<T> result = new Result<>();
        result.setCode(ResultStatus.UNLOGIN.getCode());
        result.setMessage("用户未登陆");
        return result;
    }
}
